﻿using BamaTeaWebAPI.Models;
using BamaTeaWebAPI.Models.FXModels;
using CSCommonUtils;
using FX_API.Models;
using FX_API.Request;
using log4net;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace BamaTeaWebAPI.Services
{
    public class WutongService
    {
        private ILog logger = LogManager.GetLogger("WutongService");
        protected FXLeadsQueryService fxLeadsQueryService = new FXLeadsQueryService();

        public bool UploadToFX(WutongRequestModel requestModel)
        {
            var telData = requestModel.data.Where(item => item.name == "电话" || item.name == "请输入电话").FirstOrDefault()?.value;
            if (telData != null)
            {
                var lead = fxLeadsQueryService.GetLeadByTel(telData);
                if (lead != null)
                {
                    logger.Info($"从飞鱼上传信息出现重复手机号, 数据信息: {JsonConvert.SerializeObject(requestModel)}");
                    return true;
                }
            }

            // 获取纷享的请求对象
            var fxRequestModel = Map2FXLeadsModel(requestModel);

            FXCommonUploadRequest uploadRequest = new FXCommonUploadRequest();
            uploadRequest.exceptionHandler += (Exception ex) =>
            {
                logger.Error($"从百度上传信息失败，错误信息: {ex.Message}, 错误堆栈: {ex.StackTrace}, 数据信息: {JsonConvert.SerializeObject(requestModel)}");
            };
            uploadRequest.httpErrHandler += (string responseMsg, FXCreateRequestModel request) =>
            {
                logger.Error($"从百度上传信息失败，返回信息: {responseMsg}数据信息: {JsonConvert.SerializeObject(requestModel)}");
            };
            var result = uploadRequest.Upload(fxRequestModel);

            if (result != null && result.errorCode != 0)
            {
                logger.Info($"从百度上传到纷享发生错误, 返回信息: {JsonConvert.SerializeObject(result)} 数据信息: {JsonConvert.SerializeObject(requestModel)}");
            }

            logger.Info($"从百度上传到纷享成功，数据信息: {JsonConvert.SerializeObject(requestModel)}");

            return result != null && result.errorCode == 0;
        }

        private FXLeadsObjCreateModel Map2FXLeadsModel(WutongRequestModel model)
        {
            var result = new FXLeadsObjCreateModel()
            {
                source = "3",
                field_9fehL__c = (long)TimeParser.ConvertDateTimeInt(DateTime.Now),
            };

            foreach (var data in model.data)
            {
                switch (data.name)
                {
                    case "姓名":
                        result.name = data.value;
                        break;
                    case "电话":
                    case "请输入电话":
                        result.tel = data.value;
                        break;
                    case "加盟城市":
                    case "请选择加盟省市":
                        {
                            var location_set = data.value.Split(' ');
                            if(location_set.Length == 2)
                            {
                                result.field_3TSkg__c = location_set[0];
                                result.field_d38Vb__c = location_set[1];
                            }
                            else
                            {
                                result.field_3TSkg__c = data.value;
                                result.field_d38Vb__c = data.value;
                            }
                            break;
                        }
                }
            }

            return result;
        }
    }
}